package org.theonept.travian.model;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.theonept.travian.util.HttpUtil;

public class Message {

	private String expediteur;

	private String id;

	private String sujet;
	private String message;
	private String submitText;
	private String tValue;
	private Date date;
	private Compte compte;

	/**
	 * @return the expediteur
	 */
	public String getExpediteur() {
		return expediteur;
	}

	/**
	 * @param expediteur
	 *            the expediteur to set
	 */
	public void setExpediteur(String expediteur) {
		this.expediteur = expediteur;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the sujet
	 */
	public String getSujet() {
		return sujet;
	}

	/**
	 * @param sujet
	 *            the sujet to set
	 */
	public void setSujet(String sujet) {
		this.sujet = sujet;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	public static List<Message> getMessages(Compte compte) {
		List<Message> messages = new ArrayList<Message>();
		Document document = HttpUtil.getDocumentFromUrl(compte.getUrlServeur()
				+ "nachrichten.php");

		List<Element> activeElement = document.getElementById("content")
				.getElementsByAttributeValueContaining("href",
						"nachrichten.php?id");
		for (Element element : activeElement) {
			Document documentMessage = HttpUtil.getDocumentFromUrl(compte
					.getUrlServeur() + element.attr("href"));
			Message message = new Message();
			message.setCompte(compte);
			message.setId(element.attr("href").replace("nachrichten.php?id=",
					""));
			Element messageElement = documentMessage.getElementById("content");

			message.setExpediteur(messageElement
					.getElementById("sender")
					.getElementsByAttributeValueStarting("href",
							"spieler.php?uid=").first().text());
			message.setSujet(messageElement.getElementById("subject")
					.getElementsByAttributeValue("class", "header text")
					.first().text());
			SimpleDateFormat sm = new SimpleDateFormat("dd.MM.yy hh:mm");
			try {
				message.setDate(sm.parse(messageElement.getElementById("time")
						.getElementsByAttributeValue("class", "header text")
						.first().text()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			message.setMessage(messageElement.getElementById("message").text());
			Element elementRepondre = messageElement.getElementById("answer");
			message.setSubmitText(elementRepondre
					.getElementsByAttributeValue("type", "submit").first()
					.attr("value"));
			message.settValue(elementRepondre.getElementById("t").attr("value"));

			messages.add(message);
		}

		return messages;
	}

	/**
	 * @return the compte
	 */
	public Compte getCompte() {
		return compte;
	}

	/**
	 * @param compte
	 *            the compte to set
	 */
	public void setCompte(Compte compte) {
		this.compte = compte;
	}

	public static void newMessage(Compte compte, String destinataire,
			String sujet, String message) {
		Document documentSendMessage = HttpUtil.getDocumentFromUrl(compte
				.getUrlServeur() + "nachrichten.php?t=1");
		String c = documentSendMessage.getElementById("send")
				.getElementsByAttributeValue("name", "c").first().attr("value");
		try {
			StringBuilder sbString = new StringBuilder();

			sbString.append("an=");

			sbString.append(URLEncoder.encode(destinataire, "UTF-8"));

			sbString.append("&be=");
			sbString.append(URLEncoder.encode(sujet, "UTF-8"));
			sbString.append("&message=");
			sbString.append(URLEncoder.encode(message, "UTF-8"));
			sbString.append("&c=");
			sbString.append(URLEncoder.encode(c, "UTF-8"));
			String result = HttpUtil.excutePost(compte.getUrlServeur()
					+ "nachrichten.php", sbString.toString());

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public void reply(String newMessage) {

		try {
			StringBuilder sbStringForm = new StringBuilder();

			sbStringForm.append("id=");

			sbStringForm.append(URLEncoder.encode(getId(), "UTF-8"));

			sbStringForm.append("&t=");
			sbStringForm.append(URLEncoder.encode(gettValue(), "UTF-8"));
			sbStringForm.append("&s1=");
			sbStringForm.append(URLEncoder.encode(getSubmitText(), "UTF-8"));

			String resultForm = HttpUtil.excutePost(compte.getUrlServeur()
					+ "nachrichten.php", sbStringForm.toString());
			Document documentReply = Jsoup.parse(resultForm);
			String cValue = documentReply.getElementById("send")
					.getElementsByAttributeValue("name", "c").first()
					.attr("value");
			String destinataire = documentReply.getElementById("receiver")
					.attr("value");
			String subject = documentReply.getElementById("subject")
					.getElementsByAttributeValue("type", "text").first()
					.attr("value");
			String oldMessage = documentReply.getElementById("message").text();
			StringBuilder sbString = new StringBuilder();

			sbString.append("an=");

			sbString.append(URLEncoder.encode(destinataire, "UTF-8"));

			sbString.append("&be=");
			sbString.append(URLEncoder.encode(subject, "UTF-8"));
			sbString.append("&message=");
			sbString.append(URLEncoder.encode(newMessage + "\n" + oldMessage,
					"UTF-8"));
			sbString.append("&c=");
			sbString.append(URLEncoder.encode(cValue, "UTF-8"));
			sbString.append("&p=");
			sbString.append(URLEncoder.encode(id, "UTF-8"));

			String result = HttpUtil.excutePost(compte.getUrlServeur()
					+ "nachrichten.php", sbString.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return the submitText
	 */
	public String getSubmitText() {
		return submitText;
	}

	/**
	 * @param submitText
	 *            the submitText to set
	 */
	public void setSubmitText(String submitText) {
		this.submitText = submitText;
	}

	/**
	 * @return the tValue
	 */
	public String gettValue() {
		return tValue;
	}

	/**
	 * @param tValue
	 *            the tValue to set
	 */
	public void settValue(String tValue) {
		this.tValue = tValue;
	}

	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * @param date
	 *            the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
	}
}
